import tkinter as tk
from tkinter import messagebox
import os

# 文件路径
data_file = 'C:/Users/wang/OneDrive/桌面/people.txt'

# 账户信息
valid_accounts = {"admin": "password123"}  # 账号和密码字典（示例）

# 确保数据文件存在
def ensure_file_exists():
    if not os.path.exists(data_file):
        with open(data_file, 'w') as file:
            pass


# 读取文件中的所有人员数据
def read_data():
    ensure_file_exists()
    with open(data_file, 'r') as file:
        lines = file.readlines()
    people = []
    for line in lines:
        people.append(line.strip().split(','))
    return people


# 写入人员数据到文件
def write_data(people):
    ensure_file_exists()
    with open(data_file, 'w') as file:
        for person in people:
            file.write(','.join(person) + '\n')


# 添加人员
def add_person(name, age, gender, position):
    people = read_data()
    people.append([name, age, gender, position])
    write_data(people)
    messagebox.showinfo("成功", "人员添加成功！")


# 删除人员
def delete_person(name):
    people = read_data()
    new_people = [person for person in people if person[0] != name]
    if len(new_people) == len(people):
        messagebox.showwarning("失败", "没有找到该人员！")
    else:
        write_data(new_people)
        messagebox.showinfo("成功", "人员删除成功！")


# 修改人员信息
def update_person(old_name, new_name, new_age, new_gender, new_position):
    people = read_data()
    updated = False
    for person in people:
        if person[0] == old_name:
            person[0] = new_name
            person[1] = new_age
            person[2] = new_gender
            person[3] = new_position
            updated = True
            break
    if updated:
        write_data(people)
        messagebox.showinfo("成功", "人员信息更新成功！")
    else:
        messagebox.showwarning("失败", "没有找到该人员！")


# 查询人员
def search_person(name):
    people = read_data()
    found = False
    for person in people:
        if person[0] == name:
            found = True
            messagebox.showinfo("查询结果", f"姓名: {person[0]}\n年龄: {person[1]}\n性别: {person[2]}\n职务: {person[3]}")
            break
    if not found:
        messagebox.showwarning("未找到", "没有找到该人员！")


# 登录界面设计
class LoginScreen:
    def __init__(self, root):
        self.root = root
        self.root.title("登录界面")
        self.root.geometry("300x200")

        self.label_username = tk.Label(root, text="用户名")
        self.label_username.pack()
        self.entry_username = tk.Entry(root)
        self.entry_username.pack()

        self.label_password = tk.Label(root, text="密码")
        self.label_password.pack()
        self.entry_password = tk.Entry(root, show="*")
        self.entry_password.pack()

        self.btn_login = tk.Button(root, text="登录", command=self.login)
        self.btn_login.pack()

    def login(self):
        username = self.entry_username.get()
        password = self.entry_password.get()

        if username in valid_accounts and valid_accounts[username] == password:
            self.root.destroy()  # 关闭登录窗口
            self.open_main_window()
        else:
            messagebox.showerror("登录失败", "用户名或密码错误！")

    def open_main_window(self):
        main_window = tk.Tk()
        app = SchoolPersonnelApp(main_window)
        main_window.mainloop()


# 学校人员管理系统
class SchoolPersonnelApp:
    def __init__(self, root):
        self.root = root
        self.root.title("学校人员管理系统")
        self.root.geometry("400x400")

        # 标签
        self.label_name = tk.Label(root, text="姓名")
        self.label_name.pack()
        self.entry_name = tk.Entry(root)
        self.entry_name.pack()

        self.label_age = tk.Label(root, text="年龄")
        self.label_age.pack()
        self.entry_age = tk.Entry(root)
        self.entry_age.pack()

        self.label_gender = tk.Label(root, text="性别")
        self.label_gender.pack()
        self.entry_gender = tk.Entry(root)
        self.entry_gender.pack()

        self.label_position = tk.Label(root, text="职务")
        self.label_position.pack()
        self.entry_position = tk.Entry(root)
        self.entry_position.pack()

        # 按钮区
        self.btn_add = tk.Button(root, text="添加人员", command=self.add_person)
        self.btn_add.pack()

        self.btn_delete = tk.Button(root, text="删除人员", command=self.delete_person)
        self.btn_delete.pack()

        self.btn_update = tk.Button(root, text="修改人员", command=self.update_person)
        self.btn_update.pack()

        self.btn_search = tk.Button(root, text="查询人员", command=self.search_person)
        self.btn_search.pack()

    # 添加人员
    def add_person(self):
        name = self.entry_name.get()
        age = self.entry_age.get()
        gender = self.entry_gender.get()
        position = self.entry_position.get()
        if name and age and gender and position:
            add_person(name, age, gender, position)
            self.clear_entries()
        else:
            messagebox.showwarning("输入无效", "请填写完整信息！")

    # 删除人员
    def delete_person(self):
        name = self.entry_name.get()
        if name:
            delete_person(name)
            self.clear_entries()
        else:
            messagebox.showwarning("输入无效", "请输入姓名！")

    # 修改人员信息
    def update_person(self):
        old_name = self.entry_name.get()
        new_name = self.entry_name.get()
        new_age = self.entry_age.get()
        new_gender = self.entry_gender.get()
        new_position = self.entry_position.get()

        if old_name and new_name and new_age and new_gender and new_position:
            update_person(old_name, new_name, new_age, new_gender, new_position)
            self.clear_entries()
        else:
            messagebox.showwarning("输入无效", "请填写完整信息！")

    # 查询人员
    def search_person(self):
        name = self.entry_name.get()
        if name:
            search_person(name)
        else:
            messagebox.showwarning("输入无效", "请输入姓名！")

    # 清空输入框
    def clear_entries(self):
        self.entry_name.delete(0, tk.END)
        self.entry_age.delete(0, tk.END)
        self.entry_gender.delete(0, tk.END)
        self.entry_position.delete(0, tk.END)


# 主函数
if __name__ == '__main__':
    root = tk.Tk()
    app = LoginScreen(root)
    root.mainloop()
